﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;

namespace DBUtitlity
{
    public class DbHelperSQL
    {
        //public static string connString= "server=.;database=Wuye;integrated security = ture";
        public  static readonly string connString = ConfigurationSettings.AppSettings ["connString"];
       // public static string connString = System.Configuration.ConfigurationSettings.AppSettings["connString"];
        /// <summary>
        /// 数据的增删改要调用的方法
        /// </summary>
        /// <param name="sql">要执行的sql命令</param>
        /// <param name="sqlParams">sql命令中包含的参数</param>
        /// <returns></returns>
        public static bool ExecuteSql(string sql,List <SqlParameter> sqlParams)
        {
            
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = connString;
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                //遍历传过来的SQL参数集合，将其逐一添加到Sqlcommand对象的参数及和中
                for (int i=0; i < sqlParams.Count; i++)
                    cmd.Parameters.Add(sqlParams[i]);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
 

        }
        /// <summary>
        /// 查询数据的通用方法
        /// </summary>
        /// <param name="sql">select语句</param>
        /// <returns>返回null待变失败
        /// 结果存在于返回的DATaTable中</returns>   
        public static DataTable Query(string sql)
        {
            //try
            //{
            //    string connString = ConfigurationSettings.AppSettings["connString"];
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = connString;
             
            SqlDataAdapter da = new SqlDataAdapter(sql,conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
               return ds.Tables[0];
            //}
            //    catch
            //    {
            //        return null;
        }
    }

}

